package com.jinxin.platform.cdacsrecord.core.task;

import com.jinxin.platform.cdacsrecord.core.config.ExecutorConfig;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.JurenAcsCommunityInfo;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.Constant;
import com.jinxin.platform.cdacsrecord.core.redis.RedisUtil;
import com.jinxin.platform.cdacsrecord.core.service.JurenAcsCaptureRecordService;
import com.jinxin.platform.cdacsrecord.core.service.JurenAcsCommunityService;
import com.jinxin.platform.cdacsrecord.core.service.JurenAcsOpenRecordService;
import com.jinxin.platform.cdacsrecord.core.service.impl.JurenAcsCaptureRecordServiceImpl;
import com.jinxin.platform.cdacsrecord.core.service.impl.JurenAcsCommunityServiceImpl;
import com.jinxin.platform.cdacsrecord.core.service.impl.JurenAcsOpenRecordServiceImpl;
import com.jinxin.platform.cdacsrecord.core.utils.DateUtil;
import com.jinxin.platform.cdacsrecord.core.utils.SpringContextUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-09-18
 * @Author: Lingnan
 **/
public class ScheduledJob implements Job {
    private JurenAcsOpenRecordService openRecordService =
            (JurenAcsOpenRecordService) SpringContextUtils.getBeanByClass(JurenAcsOpenRecordServiceImpl.class);
    private RedisUtil redisUtil =
            (RedisUtil)SpringContextUtils.getBeanByClass(RedisUtil.class);
    private JurenAcsCommunityService communityService =
            (JurenAcsCommunityService)SpringContextUtils.getBeanByClass(JurenAcsCommunityServiceImpl.class);


    @Override
    @Async("asyncServiceExecutor")
    public void execute(JobExecutionContext jobExecutionContext){
        long start = System.currentTimeMillis();
        jobExecutionContext.getScheduler();
        List<JurenAcsCommunityInfo> list = communityService.list(new HashMap());
        for (JurenAcsCommunityInfo communityInfo : list) {
            String companyId = communityInfo.getCompanyId();
            String startTime = "";
            String key = Constant.MODULE_PREX + "latest_open_record_time";
            if(redisUtil.hasKey(key)){
                startTime = (String) redisUtil.get(key);
            }
            startTime = StringUtils.isEmpty(startTime) ? DateUtil.getCurrentDayYMD()+" 00:00:00" : startTime;
            String endTime = DateUtil.transTimeToString(LocalDateTime.now());
            openRecordService.updateData(companyId,startTime,endTime); // 门禁记录
        }
        System.out.println("门禁记录同步耗时: " + (System.currentTimeMillis()-start)/1000/60);
    }
}
